package com.example.androiddevchallenge.screens.home

import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.paddingFromBaseline
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.example.androiddevchallenge.R
import dev.chrisbanes.accompanist.insets.LocalWindowInsets
import dev.chrisbanes.accompanist.insets.toPaddingValues

const val HOME_SCREEN = "home_screen"

@Composable
fun HomeScreen(
    viewModel: HomeViewModel
) {
    val paddingBottom =
        LocalWindowInsets.current.navigationBars
            .toPaddingValues().calculateBottomPadding()
    val plants by viewModel.plants.observeAsState(emptyList())
    Scaffold(
        backgroundColor = MaterialTheme.colors.background,
        modifier = Modifier.padding(
            top = 40.dp,
            bottom = paddingBottom
        ).fillMaxHeight(),
        bottomBar = { HomeScreenBottomNav() }
    ) {
        var searchQuery by mutableStateOf("")
        LazyColumn(modifier = Modifier.padding(bottom = 56.dp)) {
            item {
                HomeSearchField(
                    modifier = Modifier.padding(16.dp),
                    searchQuery = searchQuery,
                    onSearchQueryChange = { searchQuery = it }
                )
            }
            item {
                Text(
                    text = stringResource(id = R.string.browse_themes),
                    modifier = Modifier
                        .paddingFromBaseline(top = 32.dp)
                        .padding(horizontal = 16.dp),
                    style = MaterialTheme.typography.h1,
                    color = MaterialTheme.colors.onBackground
                )
            }

            item {
                ThemesHorizontalList()
            }
            item {
                HomeGardenHeader(modifier = Modifier.padding(horizontal = 16.dp))
            }
            item {
                Spacer(modifier = Modifier.height(16.dp))
            }
            items(plants) { plant ->
                PlantListItem(
                    plant = plant,
                    onPlantSelectionChange = { isChecked ->
                        viewModel.plantSelectionChanged(plant.id, isChecked = isChecked)
                    },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                )
                Spacer(modifier = Modifier.height(8.dp))
            }
        }
    }
}